import json

# CONFIG_FILE = 'config.json'
from pathlib import Path

# Use an absolute path for the configuration file
CONFIG_FILE = str(Path(__file__).parent / 'config.json')

def read_config():
    """Reads the configuration from the JSON file."""
    try:
        with open(CONFIG_FILE, 'r') as file:
            config = json.load(file)
            return config
    except FileNotFoundError:
        print(f"Configuration file '{CONFIG_FILE}' not found.")
        return None
    except json.JSONDecodeError:
        print(f"Error decoding JSON from the configuration file '{CONFIG_FILE}'.")
        return None
    except Exception as e:
        print(f"An unexpected error occurred: {e}")
        return None
    

def write_config(config):
    """Writes the configuration to the JSON file."""
    try:
        with open(CONFIG_FILE, 'w') as file:
            json.dump(config, file, indent=4)
    except Exception as e:
        print(f"An error occurred while writing to the configuration file '{CONFIG_FILE}': {e}")


def update_config(key, value):
    """Updates a specific key in the configuration file."""
    config = read_config()
    if config is not None:
        config[key] = value
        write_config(config)
    else:
        print("Failed to update configuration: Configuration file could not be read.")


def get_config_value(key):
    """Retrieves a specific value from the configuration file."""
    config = read_config()
    if config is not None:
        return config.get(key, None)
    else:
        print("Failed to retrieve configuration: Configuration file could not be read.")
        return None
    
def delete_config_key(key):
    """Deletes a specific key from the configuration file."""
    config = read_config()
    if config is not None:
        if key in config:
            del config[key]
            write_config(config)
        else:
            print(f"Key '{key}' not found in the configuration.")
    else:
        print("Failed to delete configuration key: Configuration file could not be read.")

def list_config_keys():
    """Lists all keys in the configuration file."""
    config = read_config()
    if config is not None:
        return list(config.keys())
    else:
        print("Failed to list configuration keys: Configuration file could not be read.")
        return None
    
def clear_config():
    """Clears the entire configuration file."""
    try:
        with open(CONFIG_FILE, 'w') as file:
            json.dump({}, file, indent=4)
    except Exception as e:
        print(f"An error occurred while clearing the configuration file '{CONFIG_FILE}': {e}")


# Example usage
# if __name__ == "__main__":
#     # Example: Update a configuration key
#     update_config('example_key', 'example_value')

    # # Example: Retrieve a configuration value
    # value = get_config_value('example_key')
    # print(f"Value for 'example_key': {value}")

    # # Example: List all configuration keys
    # keys = list_config_keys()
    # print(f"Configuration keys: {keys}")

    # # Example: Delete a configuration key
    # delete_config_key('example_key')

    # # Example: Clear the entire configuration
    # clear_config()
    # print("Configuration cleared.")

    # # Example: Read the configuration
    # config = read_config()
    # print(f"Current configuration: {config}")

    # # Example: Write a new configuration
    # new_config = {
    #     'key1': 'value1',
    #     'key2': 'value2',
    #     'key3': 'value3'
    # }
    # write_config(new_config)
    # print("New configuration written.")

    # # Example: Read the new configuration
    # config = read_config()
    # print(f"New configuration: {config}")

    # # Example: Update a configuration key
    # update_config('key1', 'new_value1')
    # print("Configuration updated.")

